package com.example.demo.util;
import com.example.demo.util.Constants.*;

import static com.example.demo.util.Constants.*;
/**
 * @author shenrui
 * @date 2021/9/21
 * @description 实现计算游戏的结果
 */
public class GameAnswerUtil {
    /**
     *
     * @param heightList
     * @return int
     * @description 实现计算游戏的结果
     */
    public static long computeAnswer(long[] heightList){
        int left,right, temporaryMaxHeight;
        long minBucketHeight,maxBucketVolume = 0;
        left = 0;
        right = GAME_AMOUNT - 1;
        while(left < right){
            //求左右两边height较短的一边
            if (heightList[left] > heightList[right]) {
                minBucketHeight = heightList[right];
            } else {
                minBucketHeight = heightList[left];
            }
            //求出此时所围成的的最大容积
            if (maxBucketVolume < minBucketHeight * (right -left))
                maxBucketVolume = minBucketHeight * (right -left);
            //求较短一边height的向中间靠拢的，下一个更长的height。
            if (heightList[left] > heightList[right]) {
                temporaryMaxHeight = right;
                while (heightList[temporaryMaxHeight] <= heightList[right]&&temporaryMaxHeight > left) {
                    temporaryMaxHeight--;
                }
                right = temporaryMaxHeight;
            } else {
                temporaryMaxHeight = left;
                while (heightList[temporaryMaxHeight] <= heightList[left] && temporaryMaxHeight < right) {
                    temporaryMaxHeight++;
                }
                left = temporaryMaxHeight;
            }
        }
        return maxBucketVolume;
    }
}
